package com.yingxin.yxerp.module.activiti.biz.service.process;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yingxin.yx.framework.commons.activiti.ApprovalStatus;
import com.yingxin.yx.framework.commons.activiti.dto.AuditDto;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yxerp.module.activiti.api.BizProcessHandler;
import com.yingxin.yxerp.module.activiti.api.FinishDateHandler;
import com.yingxin.yxerp.module.activiti.api.ProcessService;
import com.yingxin.yxerp.module.activiti.biz.entity.FinishDateApply;
import com.yingxin.yxerp.module.activiti.biz.service.IFinishDateApplyService;
import com.yingxin.yxerp.module.activiti.biz.vo.resp.FinishDateApplyRespVo;
import com.yingxin.yxerp.module.activiti.entity.FinishEntity;
import com.yingxin.yxerp.module.organization.entity.User;
import com.yingxin.yxerp.module.organization.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Component
public class FinishDateApplyProcessHandler implements BizProcessHandler {

    @Autowired
    private ProcessService processService;

    @Autowired
    private IFinishDateApplyService finishDateApplyService;

    @Autowired
    private IUserService userService;

    @Autowired
    private FinishDateHandlerRouter finishDateHandlerRouter;

    /**
     * 启动流程实例
     */
    @Transactional(rollbackFor = RuntimeException.class)
    public void startProcessInstance(FinishDateApply apply) {
        // 检查是否满足作废条件
        FinishDateHandler handler = finishDateHandlerRouter.getHandler(apply.getBusinessType());
        handler.check(apply.getBusinessId());
        // startProcessInstance
        processService.startProcessInstance(BusinessType.WJSQ, apply.getId(), apply.getApplyNo(), getVariables(apply.getId()));
    }


    @Override
    public Map<String, Object> getVariables(String businessId) {
        FinishDateApply apply = finishDateApplyService.getById(businessId);
        User creator = userService.getById(apply.getUserId());
        Map<String, Object> variables = new HashMap<>();
        if (StringUtils.isNotBlank(creator.getDeptId())) {
            variables.put("deptId", creator.getDeptId());
        }
        BusinessType businessType = BusinessType.getEnum(apply.getBusinessType());
        for (BusinessType type : BusinessType.values()){
            if (apply.getBusinessType().equals(type.getType())){
                variables.put("businessTypeList",businessType.getType());
                break;
            }
        }
        return variables;
    }

    @Override
    public void doCommonBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) {
        // update ApprovalStatus
        UpdateWrapper<FinishDateApply> uw = new UpdateWrapper<>();
        uw.lambda().set(FinishDateApply::getApprovalStatus, approvalStatus.getStatus());
        uw.lambda().eq(FinishDateApply::getId, id);
        finishDateApplyService.update(uw);
    }

    @Override
    public void doTaskApprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    /**
     * 业务方法：整个流程审核通过
     */
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void doProcessApprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {
        FinishDateApplyRespVo apply = finishDateApplyService.mapper().getVo( id );
        // 完结
        FinishDateHandler handler = finishDateHandlerRouter.getHandler( apply.getBusinessType() );
        handler.finishDate( getFinishDateHandler( apply ) );
    }

    @Override
    public void doProcessUnapprovedBiz(String id, AuditDto auditDto, ApprovalStatus approvalStatus) throws IllegalAccessException {

    }

    /**
     * 获取-传递参数的对象
     * @param apply
     * @return
     */
    private FinishEntity getFinishDateHandler(FinishDateApplyRespVo apply ){

        FinishEntity finishEntity = new FinishEntity();
        // 业务单据id
        finishEntity.setBusinessId( apply.getBusinessId() );
        // 作废备注
        finishEntity.setFinishRemarks( apply.getReason() );
        // 作废单id
        finishEntity.setFinishID( apply.getId() );
        // 作废单编号
        finishEntity.setFinishNo( apply.getApplyNo() );
        // 作废申请人id
        finishEntity.setApplyUserId( apply.getUserId().toString() );
        // 作废申请人姓名
        finishEntity.setApplyUserName( apply.getApplicantName() );

        return finishEntity;
    }

}